#coding=utf8
import pickle

from network import protocode,proto
from transport import TransportHandler,Protocol,show_msg
import gevent
import authclient
import dbclient

class ServerHandler(TransportHandler):
	def __init__(self,sock):
		super(ServerHandler,self).__init__(sock)
	
	def init_proto_handler(self):
		self._proto2handler = {
				protocode.MOVEREQ:self.on_movereq,
				protocode.LIGHT:self.on_light,
				}

	@show_msg
	def on_movereq(self,msg):
		print msg.text
		if msg.text == 'auth':
			au_msg = authclient.authproto.UserAuthReq()
			au_msg.account = msg.text
			au_msg.passwd = 'pw '+msg.text
			ret = authclient.service.Check(au_msg)
			print 'ret: ',ret.ret
		elif msg.text == 'db':
			db_q = dbclient.dbproto.DBQueryReq()
			db_q.sql = 'select * from ring_task_new where pc_id=10000322 and ring_task_id=3'
			ret = dbclient.gamedb.Query(db_q)
			db_ret = pickle.loads(ret.ret)
			print 'db ret: ',db_ret
		resp = proto.MoveResp()
		resp.text = 'received ' + msg.text
		self._send(resp)

	@show_msg
	def on_light(self,msg):
		print msg.intensity
